package com.android.functionmanagement.util;

import java.util.HashMap;

import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.android.functionmanagement.ControlCenter;
import com.android.functionmanagement.globle.ConstantGloble;
import com.android.functionmanagement.globle.HttpObserver;
import com.android.functionmanagement.globle.LogGloble;
import com.uwatch.helowin.R;

public class LoadUserInfo {
	private static final String TAG = "LoadUserInfo";
	public static void getUserInfo(final Context context,boolean force) {
		String userID = SharedPreferencesUtils.getParam(context,ConstantGloble.USERID, "").toString();
		Log.i("LoadUserInfo","userID="+userID);
		if (!force && !("".equals(userID) || "null".equals(userID) || null == userID))
			return;
		HashMap<String, Object> copyFrom = new HashMap<String, Object>();
		copyFrom.put(ConstantGloble.REQHEAD,DeviceUtils.getreqHeadContent(ConstantGloble.W002, context));
		copyFrom.put(ConstantGloble.BODY, getCurrentRequestParm(context));
		JSONObject jsonObject = new JSONObject(copyFrom);
		ControlCenter.getInstanse().requestHttp(ConstantGloble.getBaseUri(context),
				"post", jsonObject,null,null, new HttpObserver() {

					@Override
					public void showAlert() {
					}

					@Override
					public void resposeResposeCode(int code) {

					}

					@Override
					public void resposeContent(Object ob) {
						// 成功后保存数据
						DeviceUtils.saveUserSp((HashMap<String, Object>) ob, context);
						ConstantGloble.BASE_URL = SharedPreferencesUtils.getParam(context, ConstantGloble.URL,ConstantGloble.BASE_ALL_URL).toString();
						ToastUtil.showShortToast(context, context.getString(R.string.loadSuccess));
					}

					@Override
					public boolean httpRequestCallBackPre(Object resultObj) {
						if (resultObj != null) {
							if (resultObj instanceof HashMap) {
								HashMap<String, Object> hashMap = (HashMap<String, Object>) resultObj;
								if (hashMap
										.containsKey(ConstantGloble.RESPHEAD)) {
									if (hashMap.get(ConstantGloble.RESPHEAD) instanceof HashMap) {
										HashMap<String, String> resp = (HashMap<String, String>) hashMap
												.get(ConstantGloble.RESPHEAD);
										if (resp != null) {
											String respcode = resp.get(ConstantGloble.RESPCODE);
											// 非正常情况进行拦截
											if (!ConstantGloble.RESPCODE_SUCCESS
													.equals(respcode)) {
												if (ConstantGloble.DEBUG) {
													LogGloble.d(
															TAG,
															resp.get(ConstantGloble.RESPMSG));
												}
												ToastUtil.showShortToast(context, context.getString(R.string.loadFailed));
												return true;
											}
										}
									}
								}
							}
						}
						return false;
					}

					@Override
					public boolean httpRequestCallBackAfter(Object resultObj) {
						return false;
					}

					@Override
					public void hideAlert() {
					}
				}, context);
	}

	/**
	 * getCurrentRequestParm
	 * 
	 * @return
	 */
	public static HashMap<String, String> getCurrentRequestParm(Context context) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(ConstantGloble.DEVICEID, DeviceUtils.getIMEI(context));
		// map.put(ConstantGloble.USERID,
		// SharedPreferencesUtils.getParam(context, ConstantGloble.USERID,
		// "").toString());
		// map.put(ConstantGloble.USERNAME,
		// SharedPreferencesUtils.getParam(context, ConstantGloble.USERNAME,
		// "").toString());
		return map;
	}
}
